package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.dao.SUserRoleDao;
import com.fdb.efp.nls.service.domain.SUserRole;
import com.fdb.efp.nls.service.facade.SUserRoleService;
import com.fdb.efp.nls.service.vo.SUserRoleVO;
import com.fdb.efp.nls.service.vo.SUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 系统用户角色表				<p>
 * @author chenqm1
 * @date 2018-05-03
 */
@Service("sUserRoleService")
public class SUserRoleServiceImpl implements SUserRoleService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SUserRoleServiceImpl.class);

	@Autowired
	private SUserRoleDao sUserRoleDao;
	
	/**
	 * 新增操作
	 */
	public int insertSUserRole(SUserRoleVO inSUserRoleVo) {
		logger.debug("当前新增数据为:"+ inSUserRoleVo.toString());
		int num;
		try {
			SUserRole sUserRole = new SUserRole();
			beanCopy(inSUserRoleVo, sUserRole);
			num = sUserRoleDao.insertSUserRole(sUserRole);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SUserRoleVO inSUserRoleVo) {
		logger.debug("当前删除数据条件为:"+ inSUserRoleVo);
		int num;
		try {
			SUserRole sUserRole = new SUserRole();
			beanCopy(inSUserRoleVo, sUserRole);
			num = sUserRoleDao.deleteByPk(sUserRole);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserRoleVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SUserRoleVO inSUserRoleVo) {
		logger.debug("当前修改数据为:"+ inSUserRoleVo.toString());
		int num;
		try {
			SUserRole sUserRole = new SUserRole();
			beanCopy(inSUserRoleVo, sUserRole);
			num = sUserRoleDao.updateByPk(sUserRole);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSUserRoleVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SUserRoleVO queryByPk(SUserRoleVO inSUserRoleVo) {
		
		logger.debug("当前查询参数信息为:"+ inSUserRoleVo);
		try {
			SUserRole querySUserRole = new SUserRole();
			beanCopy(inSUserRoleVo, querySUserRole);
			SUserRole queryRslSUserRole = sUserRoleDao.queryByPk(querySUserRole);
			if (Objects.nonNull(queryRslSUserRole)) {
				SUserRoleVO outSUserRoleVo = beanCopy(queryRslSUserRole, new SUserRoleVO());
				logger.debug("当前查询结果为:"+ outSUserRoleVo.toString());
				return outSUserRoleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询操作
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SUserRoleVO> queryList(SUserRoleVO inSUserRoleVo) {

		logger.debug("当前查询参数信息为:"+ inSUserRoleVo);
		try {
			SUserRole querySUserRole = new SUserRole();
			beanCopy(inSUserRoleVo, querySUserRole);
            List<SUserRole> queryRslSUserRole = sUserRoleDao.queryList(querySUserRole);
			if (Objects.nonNull(queryRslSUserRole)) {
                List<SUserRoleVO> outSUserRoleVo = (List<SUserRoleVO>) beansCopy(queryRslSUserRole, SUserRoleVO.class);
				logger.debug("当前查询结果为:"+ outSUserRoleVo.toString());
				return outSUserRoleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<SUserVO> queryAdminbyLegalOrg(String legalOrgCode) {
		logger.debug("当前查询参数信息为:"+ legalOrgCode);
		try {
			if (StringUtil.isEmpty(legalOrgCode)){
				logger.debug("当前查询参数信息为空!");
				return null;
			}
			List<SUserVO> queryRslSUserRole = sUserRoleDao.queryAdminbyLegalOrg(legalOrgCode);
			if (queryRslSUserRole!=null && queryRslSUserRole.size()!=0) {
				logger.debug("当前查询结果为:"+ queryRslSUserRole.toString());
				return queryRslSUserRole;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SUserRoleVO> queryAllOwner(SUserRoleVO sUserRoleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SUserRole sUserRole = beanCopy(sUserRoleVo, new SUserRole());
		List<SUserRoleVO> list = null;
		try {
			List<SUserRole> sUserRoles = sUserRoleDao.queryAllOwnerByPage(sUserRole);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sUserRoles.size());
			pageSet(sUserRoles, sUserRole);
			list = (List<SUserRoleVO>) beansCopy(sUserRoles, SUserRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

}
